using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace OrtzEng
{
    public class RectCollision
    {
        public static bool Intersects(Rectangle a, Rectangle b)
        {
            return (a.Intersects(b));
        }
        public static Vector2 DisplaceVector(Rectangle mover, Rectangle idle, Vector2 direction)
        {
            if (!Intersects(mover, idle)) { throw new Exception("Rects do not intersect"); }

            int invadeX = 0;
            int invadeY = 0;

            if (direction.X == 0 && direction.Y > 0)
            {
                //down
                invadeY = idle.Top - mover.Bottom;
            }
            else if (direction.X == 0 && direction.Y < 0)
            {
                //up
                invadeY = idle.Bottom - mover.Top;
            }
            else if (direction.X > 0 && direction.Y == 0)
            {
                //right
                invadeX = idle.Left - mover.Right;
            }
            else if (direction.X < 0 && direction.Y == 0)
            {
                //left
                invadeX = idle.Right - mover.Left;
            }
            else if (direction.X > 0 && direction.Y > 0)
            {
                //down-right
                invadeY = idle.Top - mover.Bottom - mover.Height;
                invadeX = idle.Left - mover.Right;
            }
            else if (direction.X < 0 && direction.Y > 0)
            {
                //down-left
                if ((idle.Top - mover.Bottom) > mover.Height)
                {
                    invadeY = mover.Height;
                }
                else
                {
                    invadeY = idle.Top - mover.Bottom;
                }
                invadeX = idle.Right - mover.Left;
            }
            else if (direction.X < 0 && direction.Y < 0)
            {
                //up-left
                invadeY = idle.Bottom - mover.Top;
                invadeX = idle.Right - mover.Left;
            }
            else if (direction.X > 0 && direction.Y < 0)
            {
                //up-right
                invadeY = idle.Bottom - mover.Top;
                invadeX = idle.Left - mover.Right;
            }

            return new Vector2(invadeX, Math.Abs(invadeY));
        }
        public static Vector2 InverseHorizontal(Vector2 direction)
        {
            return new Vector2(direction.X * -1, direction.Y);
        }
        public static Vector2 InverseVertical(Vector2 direction)
        {
            return new Vector2(direction.X, direction.Y * -1);
        }
    }
}
